package cn.tedu.jsd2205the.back.end.project.service.impl;


import cn.tedu.jsd2205the.back.end.project.exception.ServiceException;
import cn.tedu.jsd2205the.back.end.project.mapper.UserLoginHistoryMapper;
import cn.tedu.jsd2205the.back.end.project.mapper.UserMapper;
import cn.tedu.jsd2205the.back.end.project.pojo.dto.UserLoginDTO;
import cn.tedu.jsd2205the.back.end.project.pojo.dto.UserRegisterDTO;
import cn.tedu.jsd2205the.back.end.project.pojo.entity.User;
import cn.tedu.jsd2205the.back.end.project.pojo.entity.UserLoginHistory;
import cn.tedu.jsd2205the.back.end.project.pojo.vo.UserSimplePhoneVO;
import cn.tedu.jsd2205the.back.end.project.pojo.vo.UserStandardVO;
import cn.tedu.jsd2205the.back.end.project.restful.ResponseCode;
import cn.tedu.jsd2205the.back.end.project.service.IUserService;
import cn.tedu.jsd2205the.back.end.project.util.IpUtils;
import cn.tedu.jsd2205the.back.end.project.util.PasswordEncoder;
import cn.tedu.jsd2205the.back.end.project.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 用户注册的逻辑业务层
 */
@Service
@Slf4j
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserLoginHistoryMapper userLoginHistoryMapper;

    @Override
    public void reg(UserRegisterDTO userRegisterDTO) {
        // 通过用户名查询用户信息
        UserStandardVO standardVO = userMapper.selectByUserName(userRegisterDTO.getUsername());
        // 通过手机号查询用户信息
        UserSimplePhoneVO userSimplePhoneVO = userMapper.selectByPhone(userRegisterDTO.getPhone());
        if (standardVO !=null){
            String message="注册失败，用户名已存在！";
            log.debug(message);
            throw new ServiceException(ResponseCode.CONFLICT,message);
        }

        if (userSimplePhoneVO !=null){
            String message="注册失败，手机号已存在！";
            log.debug(message);
            throw new ServiceException(ResponseCode.CONFLICT,message);
        }

        User user=new User();
        user.setId(UUID.randomUUID().toString());
        BeanUtils.copyProperties(userRegisterDTO,user);
        String password= user.getPassword();
        String encodePassword = PasswordEncoder.encode(password);
        user.setPassword(encodePassword);
        userMapper.insert(user);
        log.debug("注册完成！");

    }

    @Override
    public User Login(UserLoginDTO userLoginDTO) {
        UserStandardVO userStandardVO = userMapper.selectByUserName(userLoginDTO.getUsername());
        if(userStandardVO==null){
            throw new ServiceException(ResponseCode.NOT_FOUND,"登录失败,用户不存在!");
        }
        if(!PasswordEncoder.matches(userLoginDTO.getPassword(),userStandardVO.getPassword())){
            throw new ServiceException(ResponseCode.BAD_REQUEST, "登录失败，密码错误！");
        }
        UserLoginHistory history=new UserLoginHistory();

        history.setId(StringUtils.uuid());
        history.setUserId(userStandardVO.getId());
        history.setLoginIp(IpUtils.getLocalHost());
        history.setLoginTime(LocalDateTime.now());
        userLoginHistoryMapper.insert(history);


        return null;
    }
}
